﻿<%@ WebHandler Language="C#" Class="annualLeaveSearch" %>

using System;
using System.Web;
using System.Data;
using System.Text;
using Chenxin.Lib.Utility;
using System.Collections.Generic;

public class annualLeaveSearch : AjaxHandlerBase
{

    //*******************************************
    //功能概要       ：重写的请求处理方法
    //参数説明       ：context 请求的上下文
    //返回值説明     ：无
    //功能詳細       ：父类中隐藏了IHttpHandler接口方法，本类中须对本方法进行重写
    //作成者         ：he.jifeng 
    //作成日         ：2010-03-04 
    //*******************************************
    override public void RequestProcess(HttpContext context)
    {
        context.Response.AddHeader("pragma", "no-cache");
        context.Response.AddHeader("cache-control", "");
        context.Response.CacheControl = "no-cache";
        
        //处理类型
        string strDealType  = context.Request.Params["op"];

        switch (strDealType)
        {
            case "Search":
                searchVac(context);
                break;
        }
    }
   
    //*******************************************
    //功能概要       ：获得年假一览
    //参数説明       ：context 请求的上下文
    //返回值説明     ：无
    //功能詳細       ：获得年假一览
    //作成者         ：he.jifeng 
    //作成日         ：2010-03-22
    //*******************************************
    private void searchVac(HttpContext context)
    {
        string strRet;
        try
        {
            //参数列表
            System.Collections.Hashtable htParams = new System.Collections.Hashtable();
            //当前参数
            string strParam;
            //一页所显示的条数
            int intShowRows = 0;
            //当前页码
            int intCurPage = 0;
            //总记录数
            int intTotalCnt = 0;

            if (!string.IsNullOrEmpty(context.Request["userid"]))
            {
                htParams.Add("UserId", context.Request["userid"]);
            }
            else if (!string.IsNullOrEmpty(context.Request["departid"]))
            {
                htParams.Add("DepartId", context.Request["departid"]);
            }
            else if (!string.IsNullOrEmpty(context.Request["areaid"]))
            {
                htParams.Add("AreaId", context.Request["areaid"]);
            }
            else if (!string.IsNullOrEmpty(context.Request["companyid"]))
            {
                htParams.Add("CompanyId", context.Request["companyid"]);
            }
            else if (!string.IsNullOrEmpty(context.Request["cityid"]))
            {
                htParams.Add("CityId", context.Request["cityid"]);
            }
            else
            {
                htParams.Add("CompanyId", CurrentUserInfo.CompanyId.ToString());
            }
      
            //当前页数
            strParam = context.Request.Params["pageindex"];
            if (strParam != null & strParam != "")
            {
                intCurPage = Convert.ToInt32(strParam) + 1;
                htParams.Add("page", intCurPage.ToString());
            }
            //显示行数
            strParam = context.Request.Params["pagesize"];
            if (strParam != null & strParam != "")
            {
                intShowRows = Convert.ToInt32(strParam);
                htParams.Add("rows", strParam);
            }
           
            htParams.Add("sidx", "DepartName,UserName");

            //权限判断
            //查看所有权限
            if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewAllAnnnualVac))
            {
                htParams.Add("Permit", Constants.Permit_ViewAllAnnnualVac);
            }
            //查看城市权限
            else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewCityAnnnualVac))
            {
                htParams.Add("Permit", Constants.Permit_ViewCityAnnnualVac);
                htParams.Add("CurCityId", CurrentUserInfo.CityId);
            }
            //查看公司权限
            else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewCompanyAnnnualVac))
            {
                htParams.Add("Permit", Constants.Permit_ViewCompanyAnnnualVac);
                htParams.Add("CurCompanyId", CurrentUserInfo.CompanyId);
            }
            //查看部门权限
            else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewDepartAnnnualVac))
            {
                htParams.Add("Permit", Constants.Permit_ViewDepartAnnnualVac);
                htParams.Add("CurUserId", CurrentUserInfo.UserId);
            }
            //查看自己权限    
            else
            {
                htParams.Add("CurUserId", CurrentUserInfo.UserId);
            }
        
            //假期记录业务类
            Chenxin.Lib.Hr.BLL.AnnnualVacation bllVac = new Chenxin.Lib.Hr.BLL.AnnnualVacation();

            //取得总记录数
            //intTotalCnt = bllVac.getCntOfAnnnualVac(htParams);

            System.Data.DataSet ds = bllVac.getListOfAnnnualVac(htParams);

            strRet = retExecResult(Constants.ExecResult.Succ, "OK", GetJson(ds));
            context.Response.Write(strRet);
        }
        catch (Exception ex)
        {
            strRet = retExecResult(Constants.ExecResult.Error, ex.Message, "");
            context.Response.Write(strRet);
        }
    }

    //*******************************************
    //功能概要       ：转化为jqGrid的数据格式
    //参数説明       ：dtData 要转化的原始数据
    //               ：intShowRows 一页所要显示的条数
    //               ：intCurPage 当前的页码
    //               ：intTotal 数据总条数
    //返回值説明     ：无
    //功能詳細       ：转化为jqGrid的数据格式
    //作成者         ：he.jifeng 
    //作成日         ：2010-03-07 
    //*******************************************
    public Constants.Pagination GetJson(DataSet dsData)
    {
        Constants.Pagination stuPag = new Constants.Pagination();
        System.Data.DataRow row;
        
        System.Text.StringBuilder strData = new System.Text.StringBuilder();

        strData.Append("<table width=\"100%\" height=\"100%\" cellspacing=\"1\" class=\"tabsorternew\" id=\"report\">");
        strData.Append("<thead>");
        strData.Append("<tr>");
        strData.Append("<th width=\"30\">No.</th>");
        strData.Append("<th width=\"79\">用户名</th>");
        strData.Append("<th width=\"88\">部门名</th>");
        strData.Append("<th width=\"110\">年假总天数</th>");
        strData.Append("<th width=\"110\" >已请天数</th>");
        strData.Append("<th width=\"110\">剩余天数</th>");
        strData.Append("</tr>");
        strData.Append("</thead>");
        strData.Append("<tbody>");

        for (int idxRow = 0; idxRow < dsData.Tables[0].Rows.Count; idxRow++)
        {
            row = dsData.Tables[0].Rows[idxRow];

            strData.Append("<tr class=\"tr_houselist\">");

            strData.AppendFormat("<td>{0}</td>", (idxRow + 1).ToString());
            strData.AppendFormat("<td>{0}</td>", row["UserName"].ToString());
            strData.AppendFormat("<td>{0}</td>", row["DepartName"].ToString());
            strData.AppendFormat("<td>{0}</td>", row["CntOfTotal"].ToString());
            if (row["CntOfUsed"].ToString() == "")
            {
                strData.AppendFormat("<td>{0}</td>", "0.0");
            }
            else
            {
                strData.AppendFormat("<td>{0}</td>", row["CntOfUsed"].ToString());
            }
            if (row["CntOfRemain"].ToString() == "")
            {
                strData.AppendFormat("<td>{0}</td>", "0.0");
            }
            else
            {
                strData.AppendFormat("<td>{0}</td>", row["CntOfRemain"].ToString());
            }

            strData.Append("</tr>");
        }

        strData.Append("</tbody></table>");

        stuPag.count = Convert.ToInt32(dsData.Tables[1].Rows[0][0]);
        stuPag.data = strData.ToString();

        return stuPag;
    }    
}